//
//  TAMTNewsListController.swift
//  TAMeetApp
//
//  Created by Admin on 2024/12/4.
//

import UIKit

class TAMTNewsListController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource{
    
    private lazy var listCollectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: self.view.width, height: 70)
        layout.scrollDirection = .vertical
        layout.minimumLineSpacing = 12
        layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 20, right: 0)
        layout.headerReferenceSize = CGSize(width: self.view.width, height: 100)

        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.contentInsetAdjustmentBehavior = .never
        collectionView.backgroundColor = .white
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(TAMTNewsListCell.self, forCellWithReuseIdentifier: "new.cell")
        collectionView.register(TAMTNewsListTopView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "header")
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        return collectionView
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        self.navigationController?.setNavigationBarHidden(false, animated: true)
//    https://result.eolink.com/pbpvFEK3621e972473e4d633d6f07cac62524668faa11dd?uri=/news/top/square
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationItem.title = "消息中心"
        view.backgroundColor = .white
        
        view.addSubview(listCollectionView)
        listCollectionView.snp.makeConstraints { make in
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
            make.leading.trailing.equalToSuperview()
        }
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 2// 根据需求调整
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell = collectionView.dequeueReusableCell(
            withReuseIdentifier: String(describing: "new.cell"),
            for: indexPath
        ) as? TAMTNewsListCell else {
            fatalError("Unable to dequeue YOUfanGoddessGridsCell")
        }
        
        cell.armadilloImageView.isHidden = true
        if indexPath.row == 0 {
            cell.boaImageView.image = UIImage(named: "kf")
            cell.cougarLabel.text = "客服中心"
            cell.armadilloImageView.image = UIImage(named: "gfang")
            cell.armadilloImageView.isHidden = false
        }
        
        else if indexPath.row == 1 {
            cell.boaImageView.image = UIImage(named: "xttz")
            cell.cougarLabel.text = "系统通知"
            cell.armadilloImageView.image = UIImage(named: "gfang")
            cell.armadilloImageView.isHidden = false
        }
        
        // 配置 cell
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == UICollectionView.elementKindSectionHeader {
            let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "header", for: indexPath) as! TAMTNewsListTopView
            
            return header
        }
        
        fatalError("Unexpected element kind")
    }

    // MARK: - UICollectionView Delegate Methods
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let chat = TAMTNewChatController()
        if indexPath.row == 0 {
            chat.navigationItem.title = "客服中心"
        }
        else if indexPath.row == 1 {
            chat.navigationItem.title = "系统通知"
        }
        self.navigationController?.pushViewController(chat, animated: true)
        
    }
}
